package com.mlz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.db.sql.Order;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.mlz.domain.dto.PageDTO;
import com.mlz.domain.po.Address;
import com.mlz.domain.po.User;
import com.mlz.domain.query.UserQuery;
import com.mlz.domain.vo.AddressVO;
import com.mlz.domain.vo.UserVO;
import com.mlz.mapper.AddressMapper;
import com.mlz.mapper.UserMapper;
import com.mlz.service.IAddressService;
import com.mlz.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author eoron
 * @since 2025-01-10
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    @Override
    public UserVO queryUserAndAddressById(Long userId) {
        //1.查询用户
        User user = getById(userId);
        if(user == null){
            return null;
        }
        //2.查询收货地址
        List<Address> list = Db.lambdaQuery(Address.class)
                .eq(Address::getUserId, userId)
                .list();

        //3.封装UserVO
        UserVO userVO = BeanUtil.copyProperties(user, UserVO.class);
        userVO.setAddresses(BeanUtil.copyToList(list, AddressVO.class));

        return userVO;
    }


    /*@Override
    public PageDTO<UserVO> queryUsersPage(UserQuery userQuery) {
        // 1.构建条件
        Page<User> page = Page.of(userQuery.getPageNo(), userQuery.getPageSize());
        // 2.排序条件
        if (userQuery.getSortBy() != null) {
            page = page.addOrder(OrderItem.asc(userQuery.getSortBy()));
        }else {
            // 按照默认的更新时间排序
            page.addOrder(OrderItem.asc("update_time"));
        }
        // 3.查询
        page(page);

        // 4.对数据进行做非空校验
        List<User> records = page.getRecords();
        if (records == null || records.isEmpty()) {
            // 没有数据
            return new PageDTO<>(page.getTotal(),page.getPages(), Collections.emptyList());
        }

        // 有数据,进行转换
        List<UserVO> voList = BeanUtil.copyToList(records, UserVO.class);

        // 5.封装数据
        return new PageDTO<>(page.getTotal(),page.getPages(), voList);
    }*/


    @Override
    public PageDTO<UserVO> queryUsersPage(UserQuery userQuery) {
        // 1. 构建条件

        // 2. 查询

        // 3. 返回封装

        return null;
    }

}
